package com.spring.hotel.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spring.hotel.common.JsonResponse;
import com.spring.hotel.common.utils.MD5Utils;
import com.spring.hotel.common.utils.SessionUtils;
import com.spring.hotel.entity.PageDTO;
import com.spring.hotel.entity.User;
import com.spring.hotel.mapper.UserMapper;
import org.springframework.stereotype.Service;

@Service
public class UserService extends ServiceImpl<UserMapper, User> {
    //登录
    public JsonResponse login(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        System.out.println(user.getId()+user.getName()+user.getPassword());
        queryWrapper.lambda().eq(User::getId, user.getId()).eq(User::getPassword, MD5Utils.md5(user.getPassword()));
        System.out.println(user.getId());
        System.out.println(MD5Utils.md5(user.getPassword()));
        User loginUser = baseMapper.selectOne(queryWrapper);
        if (loginUser != null) {
            SessionUtils.saveCurrentUserInfo(loginUser);
            return JsonResponse.success(loginUser, "登录成功" );
        } else {
            return JsonResponse.failure("用户名或密码有误");
        }
    }

    //分页展示
    public JsonResponse pageList(User user, PageDTO pageDTO) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(StringUtils.isNotBlank(user.getId()),User::getId,user.getId());
        Page<User> page = this.page(new Page<>(pageDTO.getPageNum(), pageDTO.getPageSize()), queryWrapper);
        return JsonResponse.success(page);
    }
}
